﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DX_sharq.Models;
using MyCrypto;

namespace DX_sharq.core
{
    public class CookieClass
    {
        Linq_sharqDB1DataContext db = new Linq_sharqDB1DataContext();
        Crypto _crypto = new Crypto();
        public void WriteCookie(
            int ad,                 // Foydalanuvchi yoki Admin, 0- foydalanuvchi, 1-Admin;
            int kir,                // Kirish huquqi; 0 - kirish mumkinmas, 1-kirish mumkin;
            string ism,             // User ismi
            string fam,             // User familiyasi
            string ishchi_orni_id,       // User ishchi o`rni ID si
            string ishchi_orni_name,     // User ishchi o`rni Nomi
            string bulim_type,      // User Bulimining tipi 
            string bulim_id,        // Bulim ID si
            string bulim_name,      // Bulim nomi
            string login,           // Login
            Nullable<int> user_orig_id        // userning asl ID-si      ------  kalit muddatini tekshirish uchun
            )
        {
            HttpCookie cook = new HttpCookie("_C");         // Application admin
            HttpContext.Current.Response.Cookies.Add(cook);
            string userIDSt = Guid.NewGuid().ToString(); // SecurityFor... даги cookie билан бир хил булади
            cook.Value = userIDSt;
            HttpContext.Current.Session[userIDSt + "_app_ad"] = ad;
            HttpContext.Current.Session[userIDSt + "_app_ki"] = kir;
            
            HttpContext.Current.Session[userIDSt + "_use_i"] = ism;
            HttpContext.Current.Session[userIDSt + "_use_f"] = fam;
            HttpContext.Current.Session[userIDSt + "_use_ii"] = ishchi_orni_id;
            HttpContext.Current.Session[userIDSt + "_use_l"] = login;
            HttpContext.Current.Session[userIDSt + "_use_in"] = ishchi_orni_name;

            HttpContext.Current.Session[userIDSt + "_bul_t"] = bulim_type;
            HttpContext.Current.Session[userIDSt + "_bul_i"] = bulim_id;
            HttpContext.Current.Session[userIDSt + "_bul_n"] = bulim_name;
            HttpContext.Current.Session[userIDSt + "_user_id_orig"] = user_orig_id;

        }


        //// _Application
        //public int _Application_admin()
        //{
        //    HttpCookie cookie = HttpContext.Current.Request.Cookies["_C"];
        //    int admin = 0;
        //    if (cookie != null)
        //    {
        //        string _guid = cookie.Value;
        //        admin = (int)HttpContext.Current.Session[_guid + "_app_ad"];
        //    }
        //    else
        //    {
        //        admin = 0;
        //    }
        //    return admin;
        //}
        //public int _Application_kirish()
        //{
        //    HttpCookie cookie = HttpContext.Current.Request.Cookies["_C"];
        //    int admin = 0;
        //    if (cookie != null)
        //    {
        //        string _guid = cookie.Value;
        //        admin = (int)HttpContext.Current.Session[_guid + "_app_ki"];
        //    }
        //    else
        //    {
        //        admin = 0;
        //    }
        //    return admin;
        //}

        //// _org
       
        //// _user
        public string _user_ism()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["_C"];
            string name = "";
            if (cookie != null)
            {
                string _guid = cookie.Value;
                name = (string)HttpContext.Current.Session[_guid + "_use_i"];
            }
            else
            {
                name = "";
            }
            return name;
        }
        public string _user_fam()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["_C"];
            string name = "";
            if (cookie != null)
            {
                string _guid = cookie.Value;
                name = (string)HttpContext.Current.Session[_guid + "_use_f"];
            }
            else
            {
                name = "";
            }
            return name;
        }
        public string _user_login()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["_C"];
            string name = "";
            if (cookie != null)
            {
                string _guid = cookie.Value;
                name = (string)HttpContext.Current.Session[_guid + "_use_l"];
            }
            else
            {
                name = "";
            }
            return name;
        }
        public string _user_ishchi_orni_name()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["_C"];
            string name = "";
            if (cookie != null)
            {
                string _guid = cookie.Value;
                name = (string)HttpContext.Current.Session[_guid + "_use_in"];
            }
            else
            {
                name = "";
            }
            return name;
        }
        public int _user_ishchi_orni_id()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["_C"];
            int id = 0;
            if (cookie != null)
            {
                string _guid = cookie.Value;
                id = Convert.ToInt32(HttpContext.Current.Session[_guid + "_use_ii"]);
            }
            else
            {
                id = 0;
            }
            return id;
        }

        public int _user_orig_id()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["_C"];
            int id = 0;
            if (cookie != null)
            {
                string _guid = cookie.Value;
                id = Convert.ToInt32(HttpContext.Current.Session[_guid + "_user_id_orig"]);
            }
            else
            {
                id = 0;
            }
            return id;
        }


        //// _bilim
        public int _bulim_type()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["_C"];
            int id = 0;
            if (cookie != null)
            {
                string _guid = cookie.Value;
                id = Convert.ToInt32(HttpContext.Current.Session[_guid + "_bul_t"]);
            }
            else
            {
                id = 0;
            }
            return id;
        }
        public int _bulim_id()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["_C"];
            int id = 0;
            if (cookie != null)
            {
                string _guid = cookie.Value;
                id = Convert.ToInt32(HttpContext.Current.Session[_guid + "_bul_i"]);
            }
            else
            {
                id = 0;
            }
            return id;
        }
        public string _bulim_name()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["_C"];
            string name = "";
            if (cookie != null)
            {
                string _guid = cookie.Value;
                name = (string)HttpContext.Current.Session[_guid + "_bul_n"];
            }
            else
            {
                name = "";
            }
            return name;
        }

        //// _Document
        public int _doc_id()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["_document"];
            int id = 0;
            if (cookie != null)
            {
                id = Convert.ToInt32(_crypto.Decrypt(cookie.Value, _crypto.sharedSec));
            }
            else
            {
                id = 0;
            }
            return id;
        }

        //// _Project
        public int _project_id()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["_project"];
            int id = 0;
            if (cookie != null)
            {
                id = Convert.ToInt32(_crypto.Decrypt(cookie.Value, _crypto.sharedSec));
            }
            else
            {
                id = 0;
            }
            return id;
        }


        public void ClearCookie(string cookie_name)
        {
            HttpCookie myCookie = new HttpCookie(cookie_name);
            DateTime now = DateTime.Now;
            myCookie.Expires = now.AddMinutes(-1);
            HttpContext.Current.Response.Cookies.Add(myCookie);
        }

        public void ClearCookieAll()
        {
            //HttpContext.Current.Session.Remove(
            ClearCookie("_C");
            ClearCookie("_app_ki");
            ClearCookie("_org_i");
            ClearCookie("_org_n");
            ClearCookie("_org_o");
            ClearCookie("_org_r");
            ClearCookie("_use_i");
            ClearCookie("_use_f");
            ClearCookie("_use_l");
            ClearCookie("_use_ii");
            ClearCookie("_use_in");
            ClearCookie("_bul_t");
            ClearCookie("_bul_i");
            ClearCookie("_bul_n");
            ClearCookie("_document");
            ClearCookie("_project");
        }

        public void WriteDocSearchCookie(
            string reg_raqam,        // Tashkilot nomi
            string date1,          // Tashkilot ID si
            string date2,       // Tashkilot darajasi, 0 - qismiy tashkilot, 1-ildiz tashkilot;
            string old_reg_raqam,
            string doc_date1,
            string doc_date2,
            int card_type,            // User ismi
            bool faol
            )
        {
            HttpCookie cook = new HttpCookie("_doc_search_card");
            HttpContext.Current.Response.Cookies.Add(cook);
            cook.Value = Convert.ToString(card_type);

            cook = new HttpCookie("_doc_search_reg");
            HttpContext.Current.Response.Cookies.Add(cook);
            cook.Value = reg_raqam;

            cook = new HttpCookie("_doc_search_date1");
            HttpContext.Current.Response.Cookies.Add(cook);
            cook.Value = date1;

            cook = new HttpCookie("_doc_search_date2");
            HttpContext.Current.Response.Cookies.Add(cook);
            cook.Value = date2;

            cook = new HttpCookie("_doc_search_old_reg");
            HttpContext.Current.Response.Cookies.Add(cook);
            cook.Value = old_reg_raqam;

            cook = new HttpCookie("_doc_search_doc_date1");
            HttpContext.Current.Response.Cookies.Add(cook);
            cook.Value = doc_date1;

            cook = new HttpCookie("_doc_search_doc_date2");
            HttpContext.Current.Response.Cookies.Add(cook);
            cook.Value = doc_date2;

            cook = new HttpCookie("_doc_search_faol");
            HttpContext.Current.Response.Cookies.Add(cook);
            cook.Value = Convert.ToString(faol);
        }

        public int _search_card()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["_doc_search_card"];
            int card = 0;
            if (cookie != null)
            {
                card = Convert.ToInt32(cookie.Value);
            }
            else
            {
                card = 0;
            }
            return card;
        }

        public string _search_reg_raqam()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["_doc_search_reg"];
            string reg_raqam = "";
            if (cookie != null)
            {
                reg_raqam = cookie.Value;
            }
            else
            {
                reg_raqam = "";
            }
            return reg_raqam;
        }

        public string _search_reg_date1()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["_doc_search_date1"];
            string date1 = "";
            if (cookie != null)
            {
                date1 = cookie.Value;
            }
            else
            {
                date1 = "";
            }
            return date1;
        }

        public string _search_reg_date2()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["_doc_search_date2"];
            string date2 = "";
            if (cookie != null)
            {
                date2 = cookie.Value;
            }
            else
            {
                date2 = "";
            }
            return date2;
        }

        public string _search_old_reg_raqam()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["_doc_search_old_reg"];
            string old_reg_raqam = "";
            if (cookie != null)
            {
                old_reg_raqam = cookie.Value;
            }
            else
            {
                old_reg_raqam = "";
            }
            return old_reg_raqam;
        }

        public string _search_doc_date1()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["_doc_search_doc_date1"];
            string date1 = "";
            if (cookie != null)
            {
                date1 = cookie.Value;
            }
            else
            {
                date1 = "";
            }
            return date1;
        }

        public string _search_doc_date2()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["_doc_search_doc_date2"];
            string date2 = "";
            if (cookie != null)
            {
                date2 = cookie.Value;
            }
            else
            {
                date2 = "";
            }
            return date2;
        }

        public Nullable<bool> _doc_search_faol()
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies["_doc_search_faol"];
            string date2 = "";
            if (cookie != null)
            {
                date2 = cookie.Value;
                return Convert.ToBoolean(date2);
            }
            return null;
        }


    }
}